﻿// ***********************************************************************
// Assembly         : INNOVACALL.Framework.Common
// Author           : Lolo
// Created          : 09-17-2013
//
// Last Modified By : Lolo
// Last Modified On : 09-17-2013
// ***********************************************************************
// <copyright file="BitmapsExtensions.cs" company="">
//     Copyright (c) . All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;

namespace INNOVACALL.Framework.Common.Extensions
{
    /// <summary>
    /// Class BitmapsExtensions.
    /// </summary>
    public static class BitmapsExtensions
    {
        /// <summary>
        /// Resizes the bitmap.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        /// <param name="targetMaxWidth">Maximum width of the target.</param>
        /// <param name="targetMaxHeight">Maximum height of the target.</param>
        /// <param name="keepProportions">if set to <c>true</c> [keep proportions].</param>
        /// <returns>Bitmap.</returns>
        public static Bitmap ResizeBitmap(this Bitmap bitmap, float targetMaxWidth, float targetMaxHeight, bool keepProportions)
        {
            float fltNewWidth = targetMaxWidth;
            float fltNewHeight = targetMaxHeight;
            float fltOffsetX = 0; //Permet de centrer le Bitmap dans le cadre lorsque l'image est rectangulaire
            float fltOffsetY = 0; //Permet de centrer le Bitmap dans le cadre lorsque l'image est rectangulaire
            float fltCoef = 1;
            if (keepProportions)
            {
                if (bitmap.Width != bitmap.Height)
                {
                    if (bitmap.Width > bitmap.Height)
                    {
                        fltCoef = fltNewWidth / bitmap.Width;
                        fltNewHeight = bitmap.Height * fltCoef;
                        fltOffsetY = (targetMaxHeight - fltNewHeight) / 2;
                    }
                    else
                    {
                        fltCoef = fltNewHeight / bitmap.Height;
                        fltNewWidth = bitmap.Width * fltCoef;
                        fltOffsetX = (targetMaxWidth - fltNewWidth) / 2;
                    }
                }
            }
            Bitmap result = new Bitmap((int)targetMaxWidth, (int)targetMaxHeight);
            PointF oPoinF = new PointF(fltOffsetX, fltOffsetY);
            SizeF SizeF = new SizeF(fltNewWidth, fltNewHeight);
            RectangleF oRectangleF = new RectangleF(oPoinF, SizeF);
            /*
            using (Graphics g = System.Drawing.Graphics.FromImage((System.Drawing.Image)result))
                g.DrawImage(oBitmap, 0, 0, intNewWidth, intNewHeight);
            */
            using (Graphics g = System.Drawing.Graphics.FromImage((System.Drawing.Image)result))
                g.DrawImage(bitmap, oRectangleF);
            return result;
        }
    }
}
